%%% ======================== 包 ======================== %%%
%%% ------- 普通（无顺序要求或可按字母顺序排序） ------- %%%
\RequirePackage{algorithm}   % 伪代码
\RequirePackage{algorithmic} % 伪代码
\RequirePackage{amssymb}     % 数学符号库
\RequirePackage{array}       % 表格
\RequirePackage{booktabs}    % 表格
\RequirePackage{calc}        % 计算
\RequirePackage{changepage}  % 页面参数
\RequirePackage{emptypage}   % 让空白页真正空白
\RequirePackage{enumitem}    % 列表
\RequirePackage{environ}     % 环境
\RequirePackage{etoolbox}    % 编程
\RequirePackage{fancyhdr}    % 页眉页脚
\RequirePackage{geometry}    % 页面布局
\RequirePackage{graphicx}    % 图片
\RequirePackage{indentfirst} % 章节首段缩进
\RequirePackage{kvoptions}   % 键值对参数
\RequirePackage{listings}    % 代码块
\RequirePackage{microtype}   % 自动排版
\RequirePackage{pgffor}      % 循环命令
\RequirePackage{physics}     % 物理符号
\RequirePackage{placeins}    % 浮动体
\RequirePackage{setspace}    % 行间距
\RequirePackage{subcaption}  % 子图
\RequirePackage{tabularx}    % 表格
\RequirePackage{titlesec}    % 章节标题
\RequirePackage{tocloft}     % 目录
\RequirePackage{xcolor}      % 颜色
\RequirePackage{xeCJKfntef}  % 中文下划线
\RequirePackage{xparse}      % 高级命令
\RequirePackage{xstring}     % 字符串处理

%%% ------------------- 须在上述之后 ------------------- %%%
\RequirePackage{hyperref}    % 超链接

%%% --------------------- 带参数包 --------------------- %%%
% 参考文献处理
\RequirePackage[
  style=gb7714-2015,
  url=false,
]{biblatex}

% 浮动体标题
\RequirePackage[
  labelsep=space,
  font=small,
]{caption}

% 智能交叉引用
\RequirePackage[
  nameinlink,
]{cleveref}

%%% ---------------------- 提供包 ---------------------- %%%
\ProvidesPackage{style/swjtu}[SWJTU Thesis Style]

%%% ===================== 宏包参数 ===================== %%%
\makeatletter

\SetupKeyvalOptions{
  family=swjtu,
  prefix=swjtu@
}

\DeclareStringOption{major}
\ProcessKeyvalOptions{swjtu}

\makeatother

%%% =========== 特殊格式 （各专业自定的格式）=========== %%%
\makeatletter
%%% --------------------- 参数定义 --------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/thesisStyle,
  prefix=swjtu@thesisStyle@
}

% 扉页
\DeclareStringOption{titlePage@titTableRowHeight}
\DeclareBoolOption[false]{titlePage@personalInformationUnderlined}

% 学术诚信声明
\DeclareBoolOption[true]{declaration@titleBoldFont}

% 授权书
\DeclareBoolOption[true]{copyright@titleBoldFont}

% 任务书
\DeclareStringOption[0pt]{task@titleBeforeSkip}
\DeclareStringOption[\baselineskip]{task@titleAfterSkip}
\DeclareBoolOption[false]{task@taskOneSeparated}
\DeclareBoolOption[false]{task@taskTwoSeparated}
\DeclareBoolOption[false]{task@approver}

% 摘要
\DeclareStringOption[0pt]{abstract@titleBeforeSkip}
\DeclareStringOption[\baselineskip]{abstract@titleAfterSkip}
\DeclareStringOption[\hspace{3em}]{abstractCN@titleSpacing}
\DeclareStringOption[\enheiti]{abstractEN@titleFontType}
\DeclareStringOption[\enheiti]{abstractEN@keyWordsFontType}
\DeclareStringOption[\hspace{0.5em}]{abstractEN@keyWordsSpacing}

% 目录
\DeclareStringOption[\zihao{-4}]{contents@titleFontSize}
\DeclareStringOption[\hspace{1em}]{contents@titleSpacing}
\DeclareStringOption[0pt]{contents@titleBeforeSkip}
\DeclareStringOption[\baselineskip]{contents@titleAfterSkip}
\DeclareStringOption[0pt]{contents@chapterBeforeSkip}
\DeclareStringOption[0pt]{contents@chapterAfterSkip}
\DeclareStringOption{contents@indentType}
\DeclareStringOption[4em]{contents@margin}
\DeclareStringOption[1.5]{contents@contentsLineSpacing}

% 正文
\DeclareStringOption[1em]{mainText@chapterTitleLabelSeparation}
\DeclareStringOption[17pt]{mainText@chapterTitleBeforeSkip}
\DeclareStringOption[16.5pt]{mainText@chapterTitleAfterSkip}

%%% -------- 格式设定（按各自专业设定相关参数） -------- %%%
\IfSubStr{应用物理学}{\swjtu@major}{
  \setkeys{swjtu/thesisStyle}{titlePage@personalInformationUnderlined=true}
  \setkeys{swjtu/thesisStyle}{task@approver=true}
}{
  \IfSubStr{软件工程, 人工智能, 计算机科学与技术}{\swjtu@major}{ % 计算机与人工智能学院
    \setkeys{swjtu/thesisStyle}{titlePage@titTableRowHeight=1.2cm}
    \setkeys{swjtu/thesisStyle}{declaration@titleBoldFont=false}
    \setkeys{swjtu/thesisStyle}{copyright@titleBoldFont=false}
    \setkeys{swjtu/thesisStyle}{task@titleBeforeSkip=18pt}
    \setkeys{swjtu/thesisStyle}{task@titleAfterSkip=18pt}
    \setkeys{swjtu/thesisStyle}{task@taskOneSeparated=true}
    \setkeys{swjtu/thesisStyle}{task@taskTwoSeparated=true}
    \setkeys{swjtu/thesisStyle}{abstract@titleBeforeSkip=20pt}
    \setkeys{swjtu/thesisStyle}{abstract@titleAfterSkip=20pt}
    \setkeys{swjtu/thesisStyle}{abstractCN@titleSpacing=\hspace{0.5em}}
    \setkeys{swjtu/thesisStyle}{abstractEN@titleFontType=}
    \setkeys{swjtu/thesisStyle}{abstractEN@keyWordsFontType=}
    \setkeys{swjtu/thesisStyle}{abstractEN@keyWordsSpacing=}
    \setkeys{swjtu/thesisStyle}{contents@titleFontSize=\zihao{-2}}
    \setkeys{swjtu/thesisStyle}{contents@titleSpacing=\hspace{0.5em}}
    \setkeys{swjtu/thesisStyle}{contents@titleBeforeSkip=20pt}
    \setkeys{swjtu/thesisStyle}{contents@titleAfterSkip=20pt}
    \setkeys{swjtu/thesisStyle}{contents@chapterBeforeSkip=0.25\baselineskip}
    \setkeys{swjtu/thesisStyle}{contents@chapterAfterSkip=0.25\baselineskip}
    \setkeys{swjtu/thesisStyle}{contents@indentType=step}
    \setkeys{swjtu/thesisStyle}{contents@margin=0em}
    \setkeys{swjtu/thesisStyle}{contents@contentsLineSpacing=1.3}
    \setkeys{swjtu/thesisStyle}{mainText@chapterTitleLabelSeparation=0.5em}
    \setkeys{swjtu/thesisStyle}{mainText@chapterTitleBeforeSkip=20pt}
    \setkeys{swjtu/thesisStyle}{mainText@chapterTitleAfterSkip=8pt}
  }{
    % 其他专业的设置
  }
}
\makeatother

%%% ======================= 排版 ======================= %%%
%%% ----------------------- 页面 ----------------------- %%%
\geometry{
  paper=a4paper,
  left=2.7cm, right=2.7cm,
  top=2.6cm, bottom=2.6cm
}

\raggedbottom % 避免强制拉伸

%%% ------------------- 放宽断行策略 ------------------- %%%
\pretocmd{\section}{\sloppy}{}{}
\pretocmd{\subsection}{\sloppy}{}{}
\pretocmd{\subsubsection}{\sloppy}{}{}
\pretocmd{\paragraph}{\sloppy}{}{}
\pretocmd{\subparagraph}{\sloppy}{}{}
\sloppy

%%% ======================= 字体 ======================= %%%
%%% --------------------- 默认字体 --------------------- %%%
\setmainfont[
  Path=fonts/,
  UprightFont=TIMES,
  BoldFont=TIMESBD,
  ItalicFont=TIMESI,
  BoldItalicFont=TIMESBI
]{Times New Roman}
\setCJKmainfont[
  Path=fonts/,
  AutoFakeBold,
  AutoFakeSlant
]{simsun.ttc}
\setCJKmonofont[
  Path=fonts/,
  AutoFakeBold,
  AutoFakeSlant
]{simsun.ttc}

%%% --------------------- 中文字体 --------------------- %%%
\setCJKfamilyfont{songti}[
  Path=fonts/,
  AutoFakeBold,
  AutoFakeSlant
]{simsun.ttc}
\newcommand{\songti}{\CJKfamily{songti}}

\setCJKfamilyfont{heiti}[
  Path=fonts/,
  AutoFakeBold,
  AutoFakeSlant
]{simhei.ttf}
\newcommand{\heiti}{\CJKfamily{heiti}}

%%% --------------------- 英文字体 --------------------- %%%
\newfontfamily{\ensongti}[
  Path=fonts/,
  AutoFakeBold,
  AutoFakeSlant
]{simsun.ttc}
\newfontfamily{\enheiti}[
  Path=fonts/,
  AutoFakeBold,
  AutoFakeSlant
]{simhei.ttf}

%%% ------------------- 统一字体环境 ------------------- %%%
\NewEnviron{allsongti}{
  {\songti\ensongti\BODY}
}
\NewEnviron{allheiti}{
  {\heiti\enheiti\BODY}
}

%%% ===================== 页眉页脚 ===================== %%%
\pagestyle{fancy}
\ctexset{chapter/pagestyle=fancy}

%%% ----------------------- 页眉 ----------------------- %%%
\fancyhead[L]{}
\fancyhead[C]{
  \begin{allheiti}
    西南交通大学本科毕业设计(论文)
  \end{allheiti}
}
\fancyhead[R]{}
\renewcommand{\headrulewidth}{0.8mm}
\setlength{\headheight}{14.45398pt}

%%% ----------------------- 页脚 ----------------------- %%%
\fancyfoot[C]{
  \heiti
  第\thepage 页
}
\renewcommand{\footrulewidth}{0pt}

%%% ===================== 参考文献 ===================== %%%
%%% ------------------------ 源 ------------------------ %%%
\addbibresource{bibliography/references.bib}

%%% ------------------- 补充格式修正 ------------------- %%%
% 姓名格式
\DeclareNameFormat{family-given-init}{%
  \MakeCapital{\namepartfamily} % 姓氏首字母大写
  \ifblank{\namepartgiveni}{}{%
    \addspace\namepartgiveni} % 姓名有缩写，添加空格和缩写（带点）
  \ifnumequal{\value{listcount}}{\value{liststop}}{}{%
    \addcomma\space} % 非最后者，添加逗号和空格
  \usebibmacro{name:andothers} % 处理“et al.”等情况
}

% 应用姓名格式到作者和编辑
\DeclareNameAlias{author}{family-given-init}
\DeclareNameAlias{editor}{family-given-init}

% 文献名
\DeclareFieldFormat*{titlecase}{\MakeSentenceCase{#1}}

%%% ===================== 公式图表 ===================== %%%
\renewcommand{\theequation}{\thechapter-\arabic{equation}}
\renewcommand{\thefigure}{\thechapter-\arabic{figure}}
\renewcommand{\thetable}{\thechapter-\arabic{table}}
\renewcommand{\thealgorithm}{\thechapter-\arabic{algorithm}}

%%% ===================== 标题间距 ===================== %%%
\makeatletter

% 章标题
\titleformat{\chapter}
{\centering\zihao{-2}\heiti}
{第\thechapter 章}{\swjtu@thesisStyle@mainText@chapterTitleLabelSeparation}{}
\titlespacing*{\chapter}
{0pt}{\swjtu@thesisStyle@mainText@chapterTitleBeforeSkip - \topskip - \headsep}{\swjtu@thesisStyle@mainText@chapterTitleAfterSkip} % 修正标题前的额外间距

% 无编号章标题
\titleformat{name=\chapter,numberless}
{\centering\zihao{-2}\heiti}
{}{0pt}{}
\titlespacing*{name=\chapter,numberless}
{0pt}{\swjtu@thesisStyle@mainText@chapterTitleBeforeSkip - \topskip - \headsep}{\swjtu@thesisStyle@mainText@chapterTitleAfterSkip} % 修正标题前的额外间距

% 节标题
\titleformat{\section}
{\zihao{-3}\heiti}
{\thesection}{0.5em}{}
\titlespacing*{\section}
{0pt}{13pt}{13pt}

% 子节标题
\titleformat{\subsection}
{\zihao{4}\heiti}
{\thesubsection}{0.5em}{}
\titlespacing*{\subsection}
{0pt}{10pt}{10pt}

% 子子节标题
\titleformat{\subsubsection}
{\zihao{-4}\heiti}
{}{}{}
\titlespacing*{\subsubsection}
{0pt}{0pt}{0pt}

\makeatother

%%% ====================== 代码框 ====================== %%%
\makeatletter

\definecolor{code@green}{rgb}{0,0.6,0}
\definecolor{code@gray}{rgb}{0.5,0.5,0.5}
\definecolor{code@purple}{rgb}{0.58,0,0.82}
\definecolor{code@backcolour}{rgb}{0.95,0.95,0.92}

\lstnewenvironment{swjtuCodeBlock}[1][]{
  \lstset{
    backgroundcolor=\color{code@backcolour},
    commentstyle=\color{code@green},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{code@gray},
    stringstyle=\color{code@purple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    #1
  }
  \renewcommand{\lstlistingname}{代码}
}{
}
\makeatother

%%% ====================== 伪代码 ====================== %%%
\floatname{algorithm}{算法}
\newcommand{\Input}{\STATE {\bfseries 输入：}}
\newcommand{\Output}{\STATE {\bfseries 输出：}}

%%% ===================== 公式注释 ===================== %%%
\makeatletter

\newcounter{swjtu@itemCount}
\newcounter{swjtu@currentIndex}
\newlength{\swjtu@labelwidth}

\newcommand{\swjtu@countItem}[1]{\IfBlankF{#1}{\stepcounter{swjtu@itemCount}}}
\newcommand{\swjtu@stepCurrent}{\stepcounter{swjtu@currentIndex}}

\NewDocumentCommand{\swjtuExplanation}{>{\SplitList{;}} m}{%
  \setcounter{swjtu@itemCount}{0}
  \setcounter{swjtu@currentIndex}{0}%
  \ProcessList{#1}{\swjtu@countItem}%
  \ProcessList{#1}{\swjtu@splitSymbolAndExplanationPair}%
}

\NewDocumentCommand{\swjtu@splitSymbolAndExplanationPair}{>{\SplitArgument{1}{,}} m}{%
  \swjtu@stepCurrent%
  \ifnum\value{swjtu@currentIndex}>\value{swjtu@itemCount}
  \else
    \swjtu@explainSymbol #1%
  \fi
}

\NewDocumentCommand{\swjtu@explainSymbol}{mm}{%
  \settowidth{\swjtu@labelwidth}{\hspace{6em}#1}
  \begin{explainSymbol}[labelwidth=\swjtu@labelwidth]
    \ifnum\value{swjtu@currentIndex}=1
    \swjtu@explainItem{式中\hspace{2em}#1——}{#2}
    \else
    \swjtu@explainItem{\hspace{4em}#1——}{#2}
    \fi
  \end{explainSymbol}
}

\newcommand{\swjtu@explainItem}[2]{
  \ifnum\value{swjtu@currentIndex}<\value{swjtu@itemCount}
    \item[#1] #2；
  \else
    \item[#1] #2。
  \fi
}
\makeatother

%%% ======================= 杂项 ======================= %%%
%%% ---------------------- 超链接 ---------------------- %%%
\crefname{equation}{式}{式}
\crefrangeformat{equation}{式(#3#1#4)到(#5#2#6)}
\crefmultiformat{equation}{式#2#1#3}{和#2#1#3}{、#2#1#3}{和#2#1#3}

\crefname{figure}{图}{图}
\crefrangeformat{figure}{图#3#1#4到#5#2#6}
\crefmultiformat{figure}{图#2#1#3}{和#2#1#3}{、#2#1#3}{和#2#1#3}

\crefname{table}{表}{表}
\crefrangeformat{table}{表#3#1#4到#5#2#6}
\crefmultiformat{table}{表#2#1#3}{和#2#1#3}{、#2#1#3}{和#2#1#3}

\crefname{listing}{代码}{代码}
\crefrangeformat{listing}{代码#3#1#4到#5#2#6}
\crefmultiformat{listing}{代码#2#1#3}{和#2#1#3}{、#2#1#3}{和#2#1#3}

\crefname{algorithm}{算法}{算法}
\crefrangeformat{algorithm}{算法#3#1#4到#5#2#6}
\crefmultiformat{algorithm}{算法#2#1#3}{和#2#1#3}{、#2#1#3}{和#2#1#3}

% 适配 hyperref 的 \autoref
\renewcommand{\equationautorefname}{式}
\renewcommand{\figureautorefname}{图}
\renewcommand{\tableautorefname}{表}
\providecommand{\listingautorefname}{代码}
\renewcommand{\listingautorefname}{代码}
\providecommand{\algorithmautorefname}{算法}
\renewcommand{\algorithmautorefname}{算法}

%%% ---------------------- 下划线 ---------------------- %%%
\renewcommand{\underline}{\CJKunderline[skip=false]}

%%% ----------------------- 列表 ----------------------- %%%
\newlist{explainSymbol}{itemize}{1}
\setlist[enumerate]{nosep}
\setlist[itemize]{nosep}
\setlist[description]{
  labelindent=2em,
  labelsep=1em,
  leftmargin=0pt,
  nosep,
}
\setlist[explainSymbol]{
  font=\normalfont,
  labelindent=2em,
  labelsep=0pt,
  leftmargin=!,
  align=parleft,
  nosep
}

%%% ------------------------ 表格 ---------------------- %%%
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

%%% ----------------------- 符号 ----------------------- %%%
\newcommand{\boxsurd}{ % 复选框：已选中
  \rlap{\(\Box\)}{\raisebox{0.1em}{\hspace{-0.1em}\(\surd\)}}
}

%%% ---------------------- 空白页 ---------------------- %%%
% 空
\NewEnviron{emptyPage}[1][]{
  \clearpage
  \thispagestyle{empty}
  \BODY
  \clearpage
}

% 带页眉页脚
\NewEnviron{fancyPage}[1][]{
  \clearpage
  \thispagestyle{fancy}
  \BODY
  \clearpage
}

%%% =================== 测量最大长度 =================== %%%
\makeatletter
%%% ----------------------- 参数 ----------------------- %%%
\newlength{\swjtu@measure@maxWidth}  % 最大宽度
\newlength{\swjtu@measure@tempWidth} % 临时宽度

%%% --------------------- 两两比较 --------------------- %%%
\newcommand{\swjtu@measure@getWider}[1]{%
  \settowidth{\swjtu@measure@tempWidth}{#1}

  \ifdim \swjtu@measure@tempWidth > \swjtu@measure@maxWidth
    \global\swjtu@measure@maxWidth=\swjtu@measure@tempWidth%
  \fi
}

%%% ------------------- 获取最大长度 ------------------- %%%
\newcommand{\swjtu@measure@getMaxWidth}[1]{%
  \setlength{\swjtu@measure@maxWidth}{0em}%
  \foreach \str in {#1} {%
      \swjtu@measure@getWider{\str}%
    }%
}
\makeatother

%%% ===================== 参数定义 ===================== %%%
%%% -------------------- 前置页标题 -------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/frontMatterTitle,
  prefix=swjtu@frontMatterTitle@
}
\DeclareStringOption[0pt]{beforeSkip}
\DeclareStringOption[0pt]{afterSkip}
\DeclareStringOption[\zihao{-2}]{fontsize}
\DeclareBoolOption[true]{boldfont}
\DeclareStringOption{title}
\DeclareStringOption{fonttype}

%%% ----------------------- 扉页 ----------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/titlePage,
  prefix=swjtu@titlePage@
}
\DeclareStringOption{ctitle}
\DeclareStringOption{etitle}
\DeclareStringOption{grade}
\DeclareStringOption{id}
\DeclareStringOption{name}
\DeclareStringOption{major}
\DeclareStringOption{advisor}
\DeclareStringOption{year}
\DeclareStringOption{month}

%%% ------------------- 学术诚信声明 ------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/integrity,
  prefix=swjtu@integrity@
}
\DeclareStringOption{signature}
\DeclareStringOption{year}
\DeclareStringOption{month}
\DeclareStringOption{day}

%%% ------------------- 版权使用授权 ------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/copyright,
  prefix=swjtu@copyright@
}
\DeclareStringOption{confidentialityPeriod}
\DeclareStringOption{authorSignature}
\DeclareStringOption{advisorSignature}
\DeclareStringOption{authorYear}
\DeclareStringOption{authorMonth}
\DeclareStringOption{authorDay}
\DeclareStringOption{advisorYear}
\DeclareStringOption{advisorMonth}
\DeclareStringOption{advisorDay}

%%% ---------------------- 任务书 ---------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/task,
  prefix=swjtu@task@
}
\DeclareStringOption{class}
\DeclareStringOption{name}
\DeclareStringOption{id}
\DeclareStringOption{issueYear}
\DeclareStringOption{issueMonth}
\DeclareStringOption{issueDay}
\DeclareStringOption{dueYear}
\DeclareStringOption{dueMonth}
\DeclareStringOption{dueDay}
\DeclareStringOption{title}
\DeclareStringOption{purpose}
\DeclareStringOption{tasks}
\DeclareStringOption{requirement}
\DeclareStringOption{weeks}
\DeclareStringOption{partOne}
\DeclareStringOption{partOneWeeks}
\DeclareStringOption{partTwo}
\DeclareStringOption{partTwoWeeks}
\DeclareStringOption{partThree}
\DeclareStringOption{partThreeWeeks}
\DeclareStringOption{partFour}
\DeclareStringOption{partFourWeeks}
\DeclareStringOption{partFive}
\DeclareStringOption{partFiveWeeks}
\DeclareStringOption{partReview}
\DeclareStringOption{partReviewWeeks}
\DeclareStringOption{remark}
\DeclareStringOption{advisorSignature}
\DeclareStringOption{advisorYear}
\DeclareStringOption{advisorMonth}
\DeclareStringOption{advisorDay}
\DeclareStringOption{approverSignature}
\DeclareStringOption{approverYear}
\DeclareStringOption{approverMonth}
\DeclareStringOption{approverDay}

%%% --------------------- 中文摘要 --------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/abstractCN,
  prefix=swjtu@abstractCN@
}
\DeclareStringOption{abstract}
\DeclareStringOption{keywords}

%%% --------------------- 英文摘要 --------------------- %%%
\SetupKeyvalOptions{
  family=swjtu/abstractEN,
  prefix=swjtu@abstractEN@
}
\DeclareStringOption{abstract}
\DeclareStringOption{keywords}

%%% ==================== 前置页标题 ==================== %%%
\makeatletter

\newcommand{\frontMatterTitle}[1][]{%
  \setkeys{swjtu/frontMatterTitle}{#1}

  \vspace*{\swjtu@frontMatterTitle@beforeSkip - \topskip - \headsep}\par

  {\centering \singlespacing \heiti \swjtu@frontMatterTitle@fontsize
    \ifswjtu@frontMatterTitle@boldfont
      \textbf{\swjtu@frontMatterTitle@fonttype\swjtu@frontMatterTitle@title}\par
    \else
      {\swjtu@frontMatterTitle@fonttype\swjtu@frontMatterTitle@title}\par
    \fi

    \vspace*{\swjtu@frontMatterTitle@afterSkip}\par
  }
}
\makeatother

%%% ======================= 扉页 ======================= %%%
\makeatletter

\newcommand{\swjtuTitlePage}[1][]{%
  \setkeys{swjtu/titlePage}{#1}

  \begin{titlepage}
    \singlespacing \zihao{-4}

    \begin{center}
      % 空行 * 3
      ~\\ \vspace{2\baselineskip}

      % 标题
      \begin{allheiti} \onehalfspacing \zihao{-2}
        西\hspace{0.5em}南\hspace{0.5em}交\hspace{0.5em}通\hspace{0.5em}大\hspace{0.5em}学\par
        本科毕业设计（论文）\par
      \end{allheiti}

      % 空行 * 3
      \vspace{3\baselineskip}

      % 中英文题目
      \begin{allheiti} \zihao{2}
        \swjtu@titlePage@ctitle\par
        \MakeUppercase{\swjtu@titlePage@etitle}\par
      \end{allheiti}

      % 空行 * 7
      \vspace{7\baselineskip}

      % 个人信息
      \ifdefvoid{\swjtu@thesisStyle@titlePage@titTableRowHeight}{%
        \ifswjtu@thesisStyle@titlePage@personalInformationUnderlined
          \swjtu@measure@getMaxWidth{
            \swjtu@titlePage@grade,
            \swjtu@titlePage@id,
            \swjtu@titlePage@name,
            \swjtu@titlePage@major,
            \swjtu@titlePage@advisor
          }
          \begin{allheiti}
            \begin{tabular}{lm{2em + \swjtu@measure@maxWidth}}
              年\hspace{2em}级： & \underline{\makebox[\linewidth][c]{\swjtu@titlePage@grade}}   \\
              学\hspace{2em}号： & \underline{\makebox[\linewidth][c]{\swjtu@titlePage@id}}      \\
              姓\hspace{2em}名： & \underline{\makebox[\linewidth][c]{\swjtu@titlePage@name}}    \\
              专\hspace{2em}业： & \underline{\makebox[\linewidth][c]{\swjtu@titlePage@major}}   \\
              指导教师：           & \underline{\makebox[\linewidth][c]{\swjtu@titlePage@advisor}} \\
            \end{tabular}
          \end{allheiti}
        \else
          \begin{allheiti}
            \begin{tabular}{l}
              年\hspace{2em}级：\swjtu@titlePage@grade \\
              学\hspace{2em}号：\swjtu@titlePage@id    \\
              姓\hspace{2em}名：\swjtu@titlePage@name  \\
              专\hspace{2em}业：\swjtu@titlePage@major \\
              指导老师：\swjtu@titlePage@advisor
            \end{tabular}
          \end{allheiti}
        \fi

        % 空行 * 13
        \vspace{13\baselineskip}

        % 日期
        \begin{allsongti}
          \swjtu@titlePage@year\:年\:\swjtu@titlePage@month\:月
        \end{allsongti}
      }{
        \begin{tabular}{
          >{\rule{0pt}{1.1cm}}m{2.1cm}
          >{\centering\arraybackslash\rule{0pt}{1.1cm}}m{4.0cm}
          }
          \heiti 年\hspace{2em}级: & \heiti \swjtu@titlePage@grade   \\ \cline{2-2}
          \heiti 学\hspace{2em}号: & \heiti \swjtu@titlePage@id      \\ \cline{2-2}
          \heiti 姓\hspace{2em}名: & \heiti \swjtu@titlePage@name    \\ \cline{2-2}
          \heiti 专\hspace{2em}业: & \heiti \swjtu@titlePage@major   \\ \cline{2-2}
          \heiti 指导老师:           & \heiti \swjtu@titlePage@advisor \\ \cline{2-2}
          \\
          \multicolumn{2}{c}{%
            \heiti
            \rule{0pt}{1.1cm}% 设置行高
            \swjtu@titlePage@year 年\swjtu@titlePage@month 月
          }                                                        \\
        \end{tabular}
      }
    \end{center}
  \end{titlepage}

  \thispagestyle{empty}
  \null
}
\makeatother

%%% =================== 学术诚信声明 =================== %%%
\makeatletter

\newcommand{\swjtuIntegrityDeclaration}[1][]{%
  \setkeys{swjtu/integrity}{#1}

  \begin{emptyPage}
    \doublespacing \zihao{4}

    % 标题
    \begin{center}
      \begin{allheiti} \onehalfspacing \zihao{-2}
        \ifswjtu@thesisStyle@declaration@titleBoldFont
          \textbf{西南交通大学}\par
          \textbf{本科毕业设计（论文）学术诚信声明}\par
        \else
          西南交通大学\par
          本科毕业设计（论文）学术诚信声明\par
        \fi
      \end{allheiti}
    \end{center}

    % 空行
    \vspace{\baselineskip}

    % 正文
    本人郑重声明：所呈交的毕业设计（论文），是本人在导师的指导下，独立进行研究工作所取得的成果。除文中已经注明引用的内容外，本论文不包含任何其他个人或集体已经发表或撰写过的作品成果。对本文的研究做出重要贡献的个人和集体，均已在文中以明确方式标明。本人完全意识到本声明的法律结果由本人承担。

    % 空行 * 3
    \vspace{3\baselineskip}

    % 签名
    {\noindent \hspace*{0.5\linewidth}
      作者签名：%
      \ifx\swjtu@integrity@signature\@empty\else
        \raisebox{-0.5\baselineskip}{\includegraphics[height=1.5\baselineskip]{\swjtu@integrity@signature}}
      \fi
    }

    % 空行
    \vspace{\baselineskip}

    % 日期
    \begin{allsongti} \zihao{4}
      \noindent \hspace*{0.5\linewidth}
      日期：\swjtu@integrity@year~年~\swjtu@integrity@month~月~\swjtu@integrity@day~日
    \end{allsongti}
  \end{emptyPage}
}
\makeatother

%%% =================== 版权使用授权 =================== %%%
\makeatletter

\newcommand{\swjtuCopyrightAuthorization}[1][]{%
  \setkeys{swjtu/copyright}{#1}

  \begin{emptyPage}
    \doublespacing \zihao{4}

    % 标题
    \begin{center}
      \begin{allheiti} \onehalfspacing \zihao{-2}
        \ifswjtu@thesisStyle@copyright@titleBoldFont
          \textbf{西南交通大学}\par
          \textbf{本科毕业设计（论文）版权使用授权书}\par
        \else
          西南交通大学\par
          本科毕业设计（论文）版权使用授权书\par
        \fi
      \end{allheiti}
    \end{center}

    % 空行
    \vspace{\baselineskip}

    \begin{allsongti} \zihao{4}
      % 正文
      本毕业设计（论文）作者同意学校保留并向国家有关部门或机构送交论文的复印件和电子版，允许论文被查阅和借阅。本人授权西南交通大学可以将本毕业设计（论文）的全部或部分内容编入有关数据库进行检索，可以采用影印、缩印或扫描等复制手段保存和汇编本毕业设计（论文）。

      % 保密
      \ifnum\swjtu@copyright@confidentialityPeriod>0
        \hspace{7em}\textbf{保密}\(\boxsurd\)，在~\underline{\hspace{0.5em}\swjtu@copyright@confidentialityPeriod\hspace{0.5em}}~年解密后适用本授权书。

        本论文属于

        \hspace{7em}\textbf{不保密}\(\Box\)。
      \else
        \hspace{7em}\textbf{保密}\(\Box\)，在~\underline{\hspace{2em}}~年解密后适用本授权书。

        本论文属于

        \hspace{7em}\textbf{不保密}\(\boxsurd\)。
      \fi

      （请在以上方框内打“\(\surd\)”）
    \end{allsongti}

    % 空行 * 3
    \vspace{3\baselineskip}

    % 签名与日期
    \noindent
    \begin{tabularx}{\linewidth}{@{}XX@{}}
      作者签名：%
      \ifx\swjtu@copyright@authorSignature\@empty\else
        \raisebox{-0.5\baselineskip}{
          \includegraphics[height=1.5\baselineskip, width=3cm, keepaspectratio]{\swjtu@copyright@authorSignature}
        }
      \fi
       &
      指导教师签名：%
      \ifx\swjtu@copyright@advisorSignature\@empty\else
        \raisebox{-0.5\baselineskip}{
          \includegraphics[height=1.5\baselineskip, width=3cm, keepaspectratio]{\swjtu@copyright@advisorSignature}
        }
      \fi \\
      ~
       &
      ~   \\
      日期：~\swjtu@copyright@authorYear~年~\swjtu@copyright@authorMonth~月~\swjtu@copyright@authorDay~日
       &
      日期：~\swjtu@copyright@advisorYear~年~\swjtu@copyright@advisorMonth~月~\swjtu@copyright@advisorDay~日
    \end{tabularx}
  \end{emptyPage}
}
\makeatother

%%% ====================== 任务书 ====================== %%%
\makeatletter

\newcommand{\swjtuTask}[1][]{%
  \setkeys{swjtu/task}{#1}

  \begin{fancyPage}
    \pagenumbering{Roman}

    % 标题
    \frontMatterTitle[
      beforeSkip=\swjtu@thesisStyle@task@titleBeforeSkip,
      afterSkip=\swjtu@thesisStyle@task@titleAfterSkip,
      fontsize=\zihao{3},
      title=毕业设计（论文）任务书
    ]

    \begin{allsongti}
      \setlength{\parindent}{0pt}
      \setlength{\baselineskip}{26pt}

      % 班级姓名学号
      \begin{tabularx}{\linewidth}{@{}>{\hsize=1.2\hsize}X>{\hsize=0.9\hsize}X>{\hsize=0.9\hsize}X@{}}
        班\hspace{2em}级\underline{\hspace{0.5em}\swjtu@task@class\hfill}
         &
        学生姓名\underline{\hspace{0.5em}\swjtu@task@name\hfill}
         &
        学\hspace{2em}号\underline{\hspace{0.5em}\swjtu@task@id\hfill}
      \end{tabularx}

      % 日期
      发题日期：~\swjtu@task@issueYear~年~\swjtu@task@issueMonth~月~\swjtu@task@issueDay~日
      \hfill
      完成日期：~\swjtu@task@dueYear~年~\swjtu@task@dueMonth~月~\swjtu@task@dueDay~日

      % 题目
      题\hspace{2em}目\underline{\hspace{0.5em}\swjtu@task@title\hfill}

      % 1、本设计（论文）的目的、意义
      \ifswjtu@thesisStyle@task@taskOneSeparated
        1、本设计（论文）的目的、意义\par
        \underline{\hspace{2em}\swjtu@task@purpose\hfill}\par
      \else
        1、本设计（论文）的目的、意义\underline{\hspace{0.5em}\swjtu@task@purpose\hfill}\par
      \fi

      % 2、学生应完成的任务
      \ifswjtu@thesisStyle@task@taskTwoSeparated
        2、学生应完成的任务\par
        \underline{\hspace{2em}\swjtu@task@tasks\hfill}\par
      \else
        2、学生应完成的任务\underline{\hspace{0.5em}\swjtu@task@tasks\hfill}\par
      \fi

      % 3、本设计（论文）与本专业的毕业要求达成度如何？（如在知识结构、能力结构、素质结构等方面有哪些有效的训练。）
      3、本设计（论文）与本专业的毕业要求达成度如何？（如在知识结构、能力结构、素质结构等方面有哪些有效的训练。）

      \underline{\hspace{2em}\swjtu@task@requirement\hfill}

      \newpage

      % 4、本设计（论文）各部分内容及时间分配
      4、本设计（论文）各部分内容及时间分配（共\underline{\hspace{0.5em}\swjtu@task@weeks\hspace{0.5em}}周）：

      { \setlength{\parindent}{1.5em}
      第一部分\underline{\hspace{0.5em}\swjtu@task@partOne\hfill} (\swjtu@task@partOneWeeks~周)

      第二部分\underline{\hspace{0.5em}\swjtu@task@partTwo\hfill} (\swjtu@task@partTwoWeeks~周)

      第三部分\underline{\hspace{0.5em}\swjtu@task@partThree\hfill} (\swjtu@task@partThreeWeeks~周)

      第四部分\underline{\hspace{0.5em}\swjtu@task@partFour\hfill} (\swjtu@task@partFourWeeks~周)

      第五部分\underline{\hspace{0.5em}\swjtu@task@partFive\hfill} (\swjtu@task@partFiveWeeks~周)

      评阅及答辩\underline{\hspace{0.5em}\swjtu@task@partReview\hfill} (\swjtu@task@partReviewWeeks~周)%
      }

      % 空行
      \vspace{\baselineskip}

      % 备注
      备\hspace{2em}注\underline{\hspace{0.5em}\swjtu@task@remark\hfill}

      % 空行
      \vspace{\baselineskip}

      % 指导教师与日期
      指导教师：\underline{\makebox[5em][c]{%
          \ifx\swjtu@task@advisorSignature\@empty\else
            \raisebox{-0.3\baselineskip}{
              \includegraphics[height=\baselineskip, width=3cm, keepaspectratio]{\swjtu@task@advisorSignature}
            }%
          \fi
        }}%
      \hspace{2em}~\swjtu@task@advisorYear~年~\swjtu@task@advisorMonth~月~\swjtu@task@advisorDay~日\par

      \ifswjtu@thesisStyle@task@approver
        % 审批人与日期
        \makebox[4em]{审\hfill 批\hfill 人}：\underline{\makebox[5em][c]{%
            \ifx\swjtu@task@approverSignature\@empty\else
              \raisebox{-0.3\baselineskip}{
                \includegraphics[height=\baselineskip, width=3cm, keepaspectratio]{\swjtu@task@approverSignature}
              }%
            \fi
          }}%
        \hspace{2em}~\swjtu@task@approverYear~年~\swjtu@task@approverMonth~月~\swjtu@task@approverDay~日\par
      \fi
    \end{allsongti}
  \end{fancyPage}
}
\makeatother

%%% ===================== 中文摘要 ===================== %%%
\makeatletter

\newcommand{\swjtuAbstractCN}[1][]{%
  \setkeys{swjtu/abstractCN}{#1}

  \ifodd\value{page}
  \else
    \addtocounter{page}{-1}
    \begin{emptyPage}
      \null
    \end{emptyPage}
  \fi

  % 标题
  \frontMatterTitle[
    beforeSkip=\swjtu@thesisStyle@abstract@titleBeforeSkip,
    afterSkip=\swjtu@thesisStyle@abstract@titleAfterSkip,
    boldfont=false,
    title=摘\swjtu@thesisStyle@abstractCN@titleSpacing 要
  ]

  % 摘要正文
  \swjtu@abstractCN@abstract

  % 空行
  \vspace{\baselineskip}

  % 关键词
  {\heiti
    \noindent
    关键词：\hspace{0em} \songti \swjtu@abstractCN@keywords
  }

}
\makeatother

%%% ===================== 英文摘要 ===================== %%%
\makeatletter

\newcommand{\swjtuAbstractEN}[1][]{%
  \setkeys{swjtu/abstractEN}{#1}

  \begin{fancyPage}
    % 标题
    \frontMatterTitle[
      beforeSkip=\swjtu@thesisStyle@abstract@titleBeforeSkip,
      afterSkip=\swjtu@thesisStyle@abstract@titleAfterSkip,
      boldfont=false,
      fonttype=\swjtu@thesisStyle@abstractEN@titleFontType,
      title=Abstract
    ]

    % 摘要正文
    \swjtu@abstractEN@abstract

    % 空行
    \vspace{\baselineskip}

    % 关键词
    \noindent
    \textbf{\swjtu@thesisStyle@abstractEN@keyWordsFontType Key\swjtu@thesisStyle@abstractEN@keyWordsSpacing words:} \swjtu@abstractEN@keywords
  \end{fancyPage}
}
\makeatother

%%% ======================= 目录 ======================= %%%
\makeatletter
%%% --------------------- 设置页面 --------------------- %%%
\tocloftpagestyle{fancy}

%%% ------------------- 去除自带标题 ------------------- %%%
\renewcommand{\contentsname}{}
\setlength{\cftbeforetoctitleskip}{0pt}
\setlength{\cftaftertoctitleskip}{0pt}

%%% --------------------- 条目字体 --------------------- %%%
% 章目录项
\renewcommand{\cftchapfont}{\heiti}          % 标题
\renewcommand{\cftchappagefont}{\rmfamily}   % 页码
\ctexset{chapter/number = \arabic{chapter}}  % 章编号格式

% 节目录项
\renewcommand{\cftsecfont}{\songti}          % 标题
\renewcommand{\cftsecpagefont}{\rmfamily}    % 页码

% 子节目录项
\renewcommand{\cftsubsecfont}{\songti}       % 标题
\renewcommand{\cftsubsecpagefont}{\rmfamily} % 页码

%%% -------------------- 编号盒宽度 -------------------- %%%
\setlength{\cftchapnumwidth}{0em}    % 章
\setlength{\cftsecnumwidth}{0em}     % 节
\setlength{\cftsubsecnumwidth}{0em}  % 子节

%%% ------------------- 编号标题间隔 ------------------- %%%
\renewcommand{\cftchapaftersnum}{\hspace{0.5em}}   % 章
\renewcommand{\cftsecaftersnum}{\hspace{0.5em}}    % 节
\renewcommand{\cftsubsecaftersnum}{\hspace{0.5em}} % 子节

%%% ----------------------- 缩进 ----------------------- %%%
\ifdefvoid{\swjtu@thesisStyle@contents@indentType}{%
  \setlength{\cftchapindent}{0em}   % 章
  \setlength{\cftsecindent}{0em}    % 节
  \setlength{\cftsubsecindent}{2em} % 子节
}{%
  \setlength{\cftchapindent}{0em}   % 章
  \setlength{\cftsecindent}{2em}    % 节
  \setlength{\cftsubsecindent}{4em} % 子节
}

%%% --------------------- 目录间距 --------------------- %%%
\setlength{\cftbeforechapskip}{\swjtu@thesisStyle@contents@chapterBeforeSkip}
\renewcommand{\cftchapafterpnum}{\par\vspace{\swjtu@thesisStyle@contents@chapterAfterSkip}}

%%% ---------------------- 引导符 ---------------------- %%%
\renewcommand{\cftdotsep}{1}                           % 点线密度
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % 章引导符

%%% ----------------------- 命令 ----------------------- %%%
\newcommand{\swjtuTableOfContents}{%
  \begin{fancyPage}

    % 标题
    \frontMatterTitle[
      beforeSkip=\swjtu@thesisStyle@contents@titleBeforeSkip,
      afterSkip=\swjtu@thesisStyle@contents@titleAfterSkip - \swjtu@thesisStyle@contents@chapterBeforeSkip,
      fontsize=\swjtu@thesisStyle@contents@titleFontSize,
      boldfont=false,
      title=目\swjtu@thesisStyle@contents@titleSpacing 录
    ]

    % 正文
    \begin{adjustwidth}{\swjtu@thesisStyle@contents@margin}{\swjtu@thesisStyle@contents@margin}
      \begin{spacing}{\swjtu@thesisStyle@contents@contentsLineSpacing}
        \tableofcontents
      \end{spacing}
    \end{adjustwidth}

  \end{fancyPage}
}
\makeatother

%%% ======================= 正文 ======================= %%%
\newcommand{\swjtuIntroduction}[1]{
  \cleardoublepage
  \pagenumbering{arabic} % 重置页码
  \chapter{#1}
} % 绪论章节

\newcommand{\swjtuChapter}[1]{\chapter{#1}} % 正常章节

%%% ======================= 结论 ======================= %%%
\newcommand{\swjtuConclusion}[1]{%
  \clearpage
  \phantomsection % 添加锚点
  \chapter*{#1}
  \addcontentsline{toc}{chapter}{#1} % 添加到目录

  % 修改结论节号格式
  \setcounter{section}{0}
  \renewcommand{\thesection}{\arabic{section}}
}

%%% ======================= 致谢 ======================= %%%
\newcommand{\swjtuAcknowledgments}[1]{\swjtuConclusion{#1}}

%%% ===================== 参考文献 ===================== %%%
\newcommand{\swjtuBibliography}[1]{%
  \newpage
  \phantomsection % 添加锚点
  \addcontentsline{toc}{chapter}{#1} % 添加到目录
  \printbibliography
}

%%% ======================= 附录 ======================= %%%
%%% --------------------- 条件定义 --------------------- %%%
\newif\ifinswjtuappendix
\inswjtuappendixfalse

%%% ----------------------- 命令 ----------------------- %%%
\makeatletter

\newcommand{\swjtuAppendix}[1]{%
  \ifinswjtuappendix
    % 已经切换到附录模式
  \else
    % 切换到附录模式
    \ctexset{chapter/name = {附录}}
    \titleformat{\chapter}
    {\centering\zihao{-2}\heiti}
    {附录\thechapter}{\swjtu@thesisStyle@mainText@chapterTitleLabelSeparation}{}

    \setcounter{chapter}{0}
    \renewcommand{\thesection}{\thechapter.\arabic{section}}
    \renewcommand{\theHchapter}{A\thechapter}
    \inswjtuappendixtrue
  \fi
  \chapter{#1}
}
\makeatother
